package com.quanquan.management.controller;

import base.BaseConst;
import com.quanquan.management.dto.SquareDTO;
import com.quanquan.management.request.SquareRequest;
import com.quanquan.management.service.SquareService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @ClassName:SquareController
 * @Description:TODO
 * @Author:xiongjunbao
 * @Date:2019/4/11 10:40
 * @Version 1.0
 */
@Controller
@RequestMapping("/square")
public class SquareController extends ManagerBaseController{

    private final SquareService squareService;

    @Autowired
    public SquareController(SquareService squareService) {
        this.squareService = squareService;
    }

    @RequestMapping("/list")
    public String list(Model model) {
        List<SquareDTO> list = squareService.list();
        model.addAttribute("pageObj",list);
        System.out.println(list);
        return "square/list";
    }

    @RequestMapping("/edit")
    public String edit(String id, Model model) {
        if(BaseConst.RequestMethod.GET.equals(request.getMethod())) {
            SquareDTO squareDTO = squareService.getById(id);
            model.addAttribute("editable",squareDTO);
            return "square/form";
        }

        return "redirect:square/list";
    }


    /**
     * 添加
     * @param requestBody
     * @param model
     * @return
     */
    @RequestMapping("/add")
    public String add(SquareRequest requestBody, Model model) {
        if (BaseConst.RequestMethod.GET.equals(this.request.getMethod())) {
            return "square/form";
        }
        boolean add = squareService.addCount(requestBody);
        if (add) {
            return "redirect:/square/list";
        } else {
            Map<String, Object> result = new HashMap<>();
            result.put("code",-1);
            result.put("msg","添加失败");
            model.addAllAttributes(result);
            return null;
        }
    }

}
